package outoforder.leetcode;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author shhjiang_001
 * @create 2020-07-06 10:13
 */
public class Solution416 {
    public boolean canPartition(int[] nums) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum = sum+nums[i];
        }
        if (sum%2 == 1) return false;
        int target = sum/2;
        boolean[][] dp = new boolean[nums.length + 1][target + 1];
        for (int i = 0; i < nums.length+1; i++) {
            dp[i][0] = true;
        }
        for (int i = 1; i <= nums.length; i++) {
            for (int j = 1; j <= target; j++) {
                if (j-nums[i-1] <0 ){
                    dp[i][j] = dp[i-1][j];
                }else {
                    dp[i][j] = dp[i-1][j] | dp[i-1][j-nums[i-1]];
                }
                if (j==target && dp[i][j]==true) return true;
            }
        }
        return dp[nums.length][target];
    }
    @Test
    public void test(){
        canPartition(new int[]{1, 5, 11, 5});
    }
}
